package com.yenlien.traceyun.plant.service;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.ProcessDetailDao;
import com.yenlien.traceyun.business.dao.StorageManageDao;
import com.yenlien.traceyun.plant.dao.PlantDao;
import com.yenlien.traceyun.plant.dao.ReapDetailDao;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SearchService {
    @Autowired
    private PlantDao plantDao;
    @Autowired
    private ReapDetailDao reapDetailDao;
    @Autowired
    private ProcessDetailDao processDetailDao;
    @Autowired
    private StorageManageDao storageManageDao;
    @Autowired
    private JdbcTemplate jdbc;

    public List<String> findSelect(JSONObject params) {
        List<String> list = new ArrayList<>();
        //类型
        String type = params.getString("type");
        //所属公司ID
        String companyId = params.getString("companyId");
        switch (type) {
            case "种植":
                list = plantDao.findSelect(companyId);
                break;
            case "采收":
                list = reapDetailDao.findSelect(companyId);
                break;
            case "初加工":
                list = processDetailDao.findSelect(companyId);
                break;
            case "采购入库":
                list = storageManageDao.findSelect(companyId);
                break;
            default:
                throw new BusinessException("A0001", "无此类型");
        }
        return list;
    }

    public JSONObject findList(JSONObject params) {
        JSONObject json = new JSONObject();
        //类型
        String type = params.getString("type");
        //品名
        String name = params.getString("name");
        //所属公司ID
        String companyId = params.getString("companyId");

        StringBuffer sql = new StringBuffer();
        switch (type) {
            case "种植":
                sql.append("select id,plant_name name,batch from t_plant where company_id ='" + companyId + "'");
                if (!StringUtils.isEmpty(name)) {
                    sql.append(" and plant_name ='" + name + "'");
                }
                break;
            case "采收":
                sql.append("select id,reap_name name,batch from t_plant_reap_detail where company_id ='" + companyId + "'");
                if (!StringUtils.isEmpty(name)) {
                    sql.append(" and reap_name ='" + name + "'");
                }
                break;
            case "初加工":
                sql.append("select id,product_name name,batch from t_plant_process_detail where company_id ='" + companyId + "'");
                if (!StringUtils.isEmpty(name)) {
                    sql.append(" and product_name ='" + name + "'");
                }
                break;
            case "采购入库":
                sql.append("select id,product_name name,storage_batch batch from t_storage_manage where company_id ='" + companyId + "' and source = '采购入库'");
                if (!StringUtils.isEmpty(name)) {
                    sql.append(" and product_name ='" + name + "'");
                }
            default:
                throw new BusinessException("A0001", "无此类型");
        }
        SqlRowSet rowSet = jdbc.queryForRowSet(sql.toString());
        List<JSONObject> jsonList = new ArrayList<>();
        while (rowSet.next()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", type);
            jsonObject.put("id", rowSet.getString("id"));
            jsonObject.put("name", rowSet.getString("name"));
            jsonObject.put("batch", rowSet.getString("batch"));
            jsonList.add(jsonObject);
        }

        StringBuffer sql1 = new StringBuffer();
        sql1.append("select count(1) from (");
        sql1.append(sql);
        sql1.append(") as t");
        SqlRowSet rowSet1 = jdbc.queryForRowSet(sql1.toString());
        int num = 0;
        while (rowSet1.next()) {
            num = rowSet1.getInt("count(1)");
        }

        json.put("code", "200");
        json.put("rows", jsonList);
        json.put("total", num);
        return json;
    }

}
